#ifndef  	NET_TOOL_H
#define 	NET_TOOL_H
#include "asyncBase.h"
#include "netComm.h"

#include <functional>
#include <map>
#include <memory>
#include <vector>



constexpr int SERIAL_BUFFER_SIZE = 4096;
using RECV_CB = std::function<int(int, char*, int)>;

// 不能用来监听server socket
class SelectHelper {
public:
	static SelectHelper& INS() { static SelectHelper ins; return ins; }
	int FD_SELECT(int fd, RECV_CB rCb);
	int FD_CANCEL(int fd);
	
private:
	SelectHelper(){
		//
	}
	SelectHelper(const SelectHelper&) = delete;
	SelectHelper& operator=(const SelectHelper) = delete;
	virtual ~SelectHelper() { }

	virtual int thrSelect();

	void selectFdSync(std::map<int, RECV_CB>& copy);

private:

	std::map<int, RECV_CB>			table_fd;
	std::vector<int> 				errFD;


	std::mutex						mutex_table_fd;
	std::unique_ptr<ThreadHelper>	thrHelper;
};



// 请勿在消息回调中关闭fd，会导致死锁
class SelectHelperV2 {
public:
	static SelectHelperV2& INS() { static SelectHelperV2 ins; return ins; }
	int FD_SELECT(int fd, cmdWorker* parse);
	// 请勿在消息回调中使用，会导致死锁
	int FD_CANCEL(int fd);
	
private:
	SelectHelperV2(){
		//
	}
	SelectHelperV2(const SelectHelperV2&) = delete;
	SelectHelperV2& operator=(const SelectHelperV2) = delete;
	virtual ~SelectHelperV2() { }

	virtual int thrSelect();

	void selectFdSync(std::map<int, cmdWorker*>& copy);

private:

	std::map<int, cmdWorker*>		table_fd;
	std::vector<int> 				errFD;


	std::mutex						mutex_table_fd;
	std::unique_ptr<ThreadHelper>	thrHelper;
};



#endif // ! NET_TOOL_H


